/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.quickorm.sample;

import com.quickorm.sample.daoimpl.UserDaoImpl;
import com.quickorm.sample.entity.Role;
import com.quickorm.sample.entity.User;
import com.quickorm.sample.serviceimpl.UserServiceImpl;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 *
 * @author aaa
 */
public class Menu1 {

    private static String roleId;

    public static void execute() {
        showAllUser();
        addSomeUser();
        showAllUser();
        delSomeUser();
        showAllUser();

        System.out.println("Press ANY key to continue.");
        try {
            System.in.read();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private static void addSomeUser() {
        roleId = UUID.randomUUID().toString().replace("-", "");

        Role role = new Role();
        role.setRoleId(roleId);
        role.setName("测试组");
        System.out.println("Adding a new Role......");
        Program.quickormTemplate.save(role);

        User userA = new User();
        userA.setRoleId(roleId);
        userA.setName("~`!@#$%^&*()_+-=\r\n\t/?\'\"]}[{||\\\\=+-_`~");
        userA.setLoginName("usera");
        userA.setPassword("xxxxxxxxxxxx");

        User userB = new User();
        userB.setRoleId(roleId);
        userB.setName("UserB");
        userB.setLoginName("userb");
        userB.setPassword("xxxxxxxxxxxx");

        System.out.println("Adding UserA......");
        Program.userService.saveOrUpdateUser(userA);
        System.out.println("Adding UserB......");
        Program.userService.saveOrUpdateUser(userB);
    }

    private static void delSomeUser() {
        System.out.println("Deleteing UserA......");
        Program.userService.deleteUserByLoginName("usera");
        System.out.println("Deleteing UserB......");
        Program.userService.deleteUserByLoginName("userb");
        System.out.println("Deleteing added Role......");
        Program.quickormTemplate.delete(Role.class, roleId);
    }

    private static void showAllUser() {
        System.out.println("Executing Query......");
        List<User> list = Program.userService.getAllUserList();

        System.out.println("Current User List:");
        System.out.println(String.format("---------- %s User(s) ----------------", list.size()));
        System.out.println("Index,UserId,Name,LoginName,Passoword");
        System.out.println("--------------------------------------------");
        for (int i = 0; i <= list.size() - 1; i++) {
            User user = list.get(i);
            System.out.println(String.format("%s,%s,%s,%s,%s", i + 1, user.getUserId(), user.getName(), user.getLoginName(), user.getPassword()));
        }
        System.out.println("--------------------------------------------");
    }
}
